{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 07 数据归一化处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最值归一化 Normalization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.random.randint(0, 100, 100) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([81, 73, 17,  1, 98, 61, 97,  4, 52, 82, 38, 45, 10, 18, 29, 36, 63,\n",
       "       12, 19, 45, 39,  3, 66, 78, 81, 76, 85, 28, 97,  1, 30, 29, 65,  2,\n",
       "        4,  7, 94, 38, 62, 76, 34, 71, 72, 54, 44, 26, 74, 23, 22, 11, 57,\n",
       "       79,  1, 53, 25,  9, 35, 13, 17, 45, 96, 66, 22, 41, 33, 83, 98, 19,\n",
       "        9, 59, 85, 56, 96, 15, 95,  1, 91, 42, 12, 63, 27, 12, 91,  3, 43,\n",
       "       80, 51,  3, 42, 27, 13, 32,  0, 58, 68, 37, 39, 21, 13, 51])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.82653061, 0.74489796, 0.17346939, 0.01020408, 1.        ,\n",
       "       0.62244898, 0.98979592, 0.04081633, 0.53061224, 0.83673469,\n",
       "       0.3877551 , 0.45918367, 0.10204082, 0.18367347, 0.29591837,\n",
       "       0.36734694, 0.64285714, 0.12244898, 0.19387755, 0.45918367,\n",
       "       0.39795918, 0.03061224, 0.67346939, 0.79591837, 0.82653061,\n",
       "       0.7755102 , 0.86734694, 0.28571429, 0.98979592, 0.01020408,\n",
       "       0.30612245, 0.29591837, 0.66326531, 0.02040816, 0.04081633,\n",
       "       0.07142857, 0.95918367, 0.3877551 , 0.63265306, 0.7755102 ,\n",
       "       0.34693878, 0.7244898 , 0.73469388, 0.55102041, 0.44897959,\n",
       "       0.26530612, 0.75510204, 0.23469388, 0.2244898 , 0.1122449 ,\n",
       "       0.58163265, 0.80612245, 0.01020408, 0.54081633, 0.25510204,\n",
       "       0.09183673, 0.35714286, 0.13265306, 0.17346939, 0.45918367,\n",
       "       0.97959184, 0.67346939, 0.2244898 , 0.41836735, 0.33673469,\n",
       "       0.84693878, 1.        , 0.19387755, 0.09183673, 0.60204082,\n",
       "       0.86734694, 0.57142857, 0.97959184, 0.15306122, 0.96938776,\n",
       "       0.01020408, 0.92857143, 0.42857143, 0.12244898, 0.64285714,\n",
       "       0.2755102 , 0.12244898, 0.92857143, 0.03061224, 0.43877551,\n",
       "       0.81632653, 0.52040816, 0.03061224, 0.42857143, 0.2755102 ,\n",
       "       0.13265306, 0.32653061, 0.        , 0.59183673, 0.69387755,\n",
       "       0.37755102, 0.39795918, 0.21428571, 0.13265306, 0.52040816])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x - np.min(x)) / (np.max(x) - np.min(x))  #向量求最值归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.random.randint(0, 100, (50, 2))  #代表50x2的拥有100个元素的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array(X, dtype=float)  #因为归一化之后的数值是浮点类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[96., 29.],\n",
       "       [ 6., 33.],\n",
       "       [38., 54.],\n",
       "       [53., 34.],\n",
       "       [70., 79.],\n",
       "       [35., 49.],\n",
       "       [53., 68.],\n",
       "       [62., 67.],\n",
       "       [85., 57.],\n",
       "       [26., 64.]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵求最值归一化\n",
    "X[:,0] = (X[:,0] - np.min(X[:,0])) / (np.max(X[:,0]) - np.min(X[:,0]))\n",
    "X[:,1] = (X[:,1] - np.min(X[:,1])) / (np.max(X[:,1]) - np.min(X[:,1]))\n",
    "# 如果有n个特征值的话， 只需要用一个for循环就好了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.01010101,  0.        ],\n",
       "       [ 0.8989899 ,  0.96907216],\n",
       "       [ 0.4040404 ,  0.05154639],\n",
       "       [ 0.24242424,  0.50515464],\n",
       "       [ 0.93939394,  0.31958763],\n",
       "       [ 0.46464646,  0.75257732],\n",
       "       [ 0.50505051,  0.75257732],\n",
       "       [ 0.8989899 ,  0.24742268],\n",
       "       [ 0.39393939,  0.51546392],\n",
       "       [ 0.54545455,  0.80412371]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAFepJREFUeJzt3X+MXNd12PHvMSUlW9TxBiEDREvKZFCaiGD9QWMhuSDQOHJcyjIgEo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fd7xuM9Dnyy//PNwLfr7vcWxvvPgPcB37rG+3cCf0rvudPvB56ZVV9KydxvBc5n5ouZ+RrwOHBoQ5tDwBf7P/8R8MGIGPbg7qYbOdbM/Fpmfr//8ml6z7UtVZVzC/BbwGeBH8yzczNQZby/Cjyamd8DyMxX5tzHaaoy3gR+rP/zu7j6Gc3FyMxvsPnzow8BX8qep4HFiPipWfSllOC+BLw88Hqtf2xom8x8A3gV+Im59G66qox10H30MoFSjRxvROwHdmXmn8yzYzNS5fy+B3hPRJyOiKcj4o659W76qoz3M8DHI2KN3hPffmM+XavFuN/viVV6zF4DDMvAN5b5VGlTgsrjiIiPA8vAz860R7O16Xgj4h3A54BPzKtDM1bl/F5Hb2rmA/Suyv5HRLw3My/NuG+zUGW89wJfyMz/GBH/lN7zmN+bmf9v9t2bu7nFqVIy9zVg18DrnVx96fZWm4i4jt7l3WaXR01VZaxExM8DnwLuysx/mFPfZmHUeN8JvBf4ekR8m9485UrBN1Wrfpb/ODNfz8yXgHP0gn2Jqoz3PuAJgMz8S+BH6e3D0kaVvt/TUEpwPwPsjYg9EXEDvRumKxvarAC/3P/5o8Cp7N/BKMzIsfanKX6fXmAveT4WRow3M1/NzO2ZuTszd9O7x3BXZq7W090tq/JZPkHvpjkRsZ3eNM2Lc+3l9FQZ73eADwJExM/QC+4X59rL+VkBfqlfNfN+4NXM/O5M/lLdd5fHuAt9J/C/6N15/1T/2EP0vujQ+0D8IXAe+J/AT9fd5xmO9c+B/wP8Vf/fSt19nuV4N7T9OgVXy1Q8vwH8J+AF4Dngnrr7POPx3gycpldJ81fAP6+7z1sY65eB7wKv08vS7wN+Dfi1gXP7aP//xXOz/Cy7QlWSWqiUaRlJ0hgM7pLUQgZ3SWohg7sktZDBXZJayOAuSS1kcJekFjK4S1IL/X+MTLBTrpDlqwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x87d2a90>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:,0], X[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.43806451612903224"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X[:,0])   # 第一列的均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.29688956772468184"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X[:,0])  #第0列的方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.47714285714285715"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2547880516631501"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X[:,1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 均值方差归一化 Standardization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = np.random.randint(0, 100, (50, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = np.array(X2, dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[44.,  5.],\n",
       "       [67., 92.],\n",
       "       [16., 19.],\n",
       "       [51., 63.],\n",
       "       [11., 99.],\n",
       "       [67., 68.],\n",
       "       [20., 57.],\n",
       "       [82., 53.],\n",
       "       [72., 53.],\n",
       "       [36., 71.]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X2[:10,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2[:,0] = (X2[:,0] - np.mean(X2[:,0])) / np.std(X2[:,0])\n",
    "X2[:,1] = (X2[:,1] - np.mean(X2[:,1])) / np.std(X2[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD8CAYAAAB+UHOxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAF59JREFUeJzt3X2sXHWdx/H3x4J4Y4xXbHnohWvL2u3aFVd0gmKTDSvEQuNSrLIB/xAU05Bdsn8t2RoSNyHZUJc/NhrY1coSYbPhYVkpdalbwa5hHwS5tUCpiFaCcu8lUMHiEht5+u4fc653epm5M3fOmfP4eSWTOw+n8/v2zJn5nt/v/B4UEZiZWfO8qegAzMysGE4AZmYN5QRgZtZQTgBmZg3lBGBm1lBOAGZmDeUEYGbWUE4AZmYN5QRgZtZQxxQdwGKWL18eq1atKjoMM7PK2Lt37y8jYsUg25Y6AaxatYqpqamiwzAzqwxJPx90WzcBmZk1lBOAmVlDOQGYmTVUJglA0k2SnpP0WI/Xz5b0oqSHk9sXsyjXzMyGl9VF4G8A1wO3LLLNf0XExzMqz8zMUsqkBhAR9wMvZPFeZmaWjzy7gZ4l6RFgFviriDiQY9mZ27Fvhut2P8Hs4SOsHB/jqg1rufCMiaLDMjMbWF4J4IfAuyLiJUkbgR3Amm4bStoCbAGYnJzMKbyl2bFvhi98cz9HXnk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.figure.Figure at 0x8d01668>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:,0], X2[:,1])\n",
    "plt.show()   #均值为0，方差为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.770761894538737e-17"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X2[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X2[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.3853809472693685e-17"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X2[:,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X2[:,1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
